package com.systemsjr.mpf.expense;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.systemsjr.mpf.IntentHandler;
import com.systemsjr.mpf.R;

public class ManageExpenseTypeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manage_expense_type);

		final ExpenseType expenseType = new ExpenseType();
		final EditText codeEditText = (EditText) findViewById(R.id.expTypeCodeEditText);
		final EditText expenseTypeEditText = (EditText) findViewById(R.id.expTypeEditText);

		Bundle bundle = getIntent().getExtras();

		if (bundle != null && bundle.size() > 3) {
			if (bundle.getLong(ExpenseType.ID) > 0) {
				expenseType.setId(bundle.getLong(ExpenseType.ID));
			}

			if (bundle.getString(ExpenseType.CODE) != null) {
				expenseType.setCode(ExpenseType.CODE);
			}

			if (bundle.getString(ExpenseType.EXPENSE_TYPE) != null) {
				expenseType.setExpenseType(ExpenseType.EXPENSE_TYPE);
			}
		}

		codeEditText.setText(expenseType.getCode());
		expenseTypeEditText.setText(expenseType.getExpenseType());

		Button saveButton = (Button) findViewById(R.id.saveExpenseTypeButton);
		saveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				expenseType.setCode(codeEditText.getText().toString());
				expenseType.setExpenseType(expenseTypeEditText.getText()
						.toString());

				ContentResolver cr = getContentResolver();
				ContentValues values = new ContentValues();
				values.put(ExpenseType.CODE, expenseType.getCode());
				values.put(ExpenseType.EXPENSE_TYPE,
						expenseType.getExpenseType());

				if (expenseType.getId() == null || expenseType.getId() == 0) {
					cr.insert(ExpenseType.CONTENT_URI, values);
					Cursor c = cr.query(ExpenseType.CONTENT_URI,
							ExpenseType.PROJECTIONS,
							"CODE = '" + expenseType.getCode() + "'", null,
							null);
					if (c.moveToFirst()) {
						expenseType.setId(c.getLong(c
								.getColumnIndex(ExpenseType.ID)));
					}
					c.close();
				} else {
					cr.update(ExpenseType.CONTENT_URI, values, "_id = "
							+ expenseType.getId(), null);
				}

				showList("", "");
			}
		});

		Button newButton = (Button) findViewById(R.id.newExpenseTypeButton);
		newButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				codeEditText.setText(null);
				expenseTypeEditText.setText(null);
				expenseType.setCode(codeEditText.getText().toString());
				expenseType.setExpenseType(expenseTypeEditText.getText()
						.toString());
			}
		});

		Button deleteButton = (Button) findViewById(R.id.deleteExpenseTypeButton);
		deleteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ContentResolver cr = getContentResolver();
				if (expenseType.getId() != null || expenseType.getId() > 0) {
					cr.delete(ExpenseType.CONTENT_URI,
							"_id = " + expenseType.getId(), null);
				}

				codeEditText.setText(null);
				expenseTypeEditText.setText(null);
				expenseType.setCode(codeEditText.getText().toString());
				expenseType.setExpenseType(expenseTypeEditText.getText()
						.toString());
				expenseType.setId(0L);
			}
		});

		Button searchButton = (Button) findViewById(R.id.searchExpenseTypeButton);
		searchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				expenseType.setCode(codeEditText.getText().toString());
				expenseType.setExpenseType(expenseTypeEditText.getText()
						.toString());

				String code = expenseType.getCode();
				String type = expenseType.getExpenseType();

				codeEditText.setText("");
				expenseTypeEditText.setText("");
				expenseType.setCode(codeEditText.getText().toString());
				expenseType.setExpenseType(expenseTypeEditText.getText()
						.toString());
				expenseType.setId(0L);

				showList(code, type);
			}
		});
	}

	private void showList(String code, String type) {
		Intent intent = new Intent(ManageExpenseTypeActivity.this,
				ExpenseTypeList.class);

		intent.putExtra(ExpenseType.CODE, code);
		intent.putExtra(ExpenseType.EXPENSE_TYPE, type);
		startActivityForResult(intent, 1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.manage_expense_type, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = IntentHandler.handleOnOptionsItemSelected(this,
				item.getItemId());
		startActivityForResult(intent, 1);
		return super.onOptionsItemSelected(item);
	}
}
